In [1]:
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
import pymc3 as pm
%matplotlib inline
sns.set(font_scale=1.5)
In [2]:
data = np.array([51.06, 55.12, 53.73, 50.24, 52.05, 56.40, 48.45,
52.34, 55.65, 51.49, 51.86, 63.43, 53.00, 56.09, 51.93, 52.31, 52.33,
57.48, 57.44, 55.14, 53.93, 54.62, 56.09, 68.58, 51.36, 55.47, 50.73,
51.94, 54.95, 50.39, 52.91, 51.5, 52.68, 47.72, 49.73, 51.82, 54.99,
52.84, 53.19, 54.52, 51.46, 53.73, 51.61, 49.81, 52.42, 54.3, 53.84,
53.16])
In [3]:
sns.kdeplot(data)
Out[3]:
In [8]:
with pm.Model() as model_g:
mu = pm.Uniform('mu', 40, 75)
sigma = pm.HalfNormal('sigma', sd=10)
y = pm.Normal('y', mu=mu, sd=sigma, observed=data)
trace_g = pm.sample(1100, chains=4)
In [9]:
pm.traceplot(trace_g)
Out[9]:
In [11]:
pm.plot_posterior(trace_g)
Out[11]:
In [12]:
pm.summary(trace_g)
Out[12]:
In [19]:
y_pred = pm.sample_ppc(trace_g, 100, model_g, size=len(data))
sns.kdeplot(data, c='b')
for i in y_pred['y']:
sns.kdeplot(i.flatten(), c='r', alpha=0.1)
plt.xlim(35, 75)
plt.title('Gaussian model', fontsize=16)
plt.xlabel('$x$', fontsize=16)
Out[19]:
In [18]:
y_pred['y'].shape
Out[18]:
In [22]:
(stats.iqr(data)+data.mean())*1.5
Out[22]:
In [23]:
np.mean(stats.t(loc=0, scale=1, df=1).rvs(100))
Out[23]:
In [31]:
with pm.Model() as model_t:
mu = pm.Uniform('mu', 40, 75)
sigma = pm.HalfNormal('sigma', sd=10)
nu = pm.Exponential('nu', 1/30)
y = pm.StudentT('y', mu=mu, sd=sigma, nu=nu, observed=data)
trace_t = pm.sample(5100, chains=8)
chain_t = trace_t[100:]
In [32]:
pm.traceplot(trace_t)
Out[32]:
In [33]:
pm.plot_posterior(trace_t)
Out[33]:
In [34]:
pm.summary(trace_t)
Out[34]:
In [36]:
y_pred = pm.sample_ppc(trace_t, 100, model_g, size=len(data))
sns.kdeplot(data, c='b')
for i in y_pred['y']:
sns.kdeplot(i.flatten(), c='r', alpha=0.1)
plt.xlim(35, 75)
plt.title('t model', fontsize=16)
plt.xlabel('$x$', fontsize=16)
Out[36]:
In [ ]:
In [ ]: